﻿

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using System.Data;
using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.product
{
    public partial class goodsCard : BaseAdmin
    {
        readonly DALGoodsCard theGoodsCard = new DALGoodsCard();
        tbGoodsCard oGoodsCard;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnDisable.OnClientClick = Grid1.GetNoSelectionAlertInTopReference("没有选中项！");
                this.btnDeleted.OnClientClick = Grid1.GetNoSelectionAlertInTopReference("没有选中项！");
                this.btnEnable.OnClientClick = Grid1.GetNoSelectionAlertInTopReference("没有选中项！");
                base.ValidateLoginRule(ERule.AddNewProduct);

                if (Request.QueryString["gid"] == null)
                {
                    ExtAspNet.Alert.Show("请先添加商品！");
                }
                else
                {
                    this.BindGrid();
                }
            }
        }

      string GetSqlWhere
        {
            get
            {
                StringBuilder strWhere = new StringBuilder();
               strWhere.AppendFormat(" cardCode like '%{0}%'", this.txtCardCode.Text.Trim());
                return strWhere.ToString();
            }
        }


    protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
    {
        DataTable table = theGoodsCard.GetListByPage(GetSqlWhere, string.Format("{0} {1}", e.SortField, e.SortDirection), Grid1.PageIndex, Grid1.PageSize).Tables[0];
        Grid1.DataSource = table;
        Grid1.DataBind();
        ViewState["sort"] = e.SortField;
        ViewState["dir"] = e.SortDirection;
    }
    private void BindGrid()
    {   // 1.设置总项数
        Grid1.RecordCount = theGoodsCard.GetRecordCount(GetSqlWhere);
        string strOrderBy = string.Format("{0} {1} "
                                           ,ViewState["sort"] == null ? null : ViewState["sort"].ToString()
                                           , ViewState["dir"] == null ? null : ViewState["dir"].ToString());
        if (strOrderBy.Length < 13)
        {
            strOrderBy = "addTime desc ";
        }
        DataTable table = theGoodsCard.GetListByPage(GetSqlWhere, strOrderBy, Grid1.PageIndex, Grid1.PageSize).Tables[0];

        Grid1.DataSource = table;
        Grid1.DataBind();
        Grid1.SelectedRowIndexArray = new int[] { };

    }



        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {

        }

        protected void btnEnable_Click(object sender, EventArgs e)
        {
            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            StringBuilder sbProIds = new StringBuilder();
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];
                if (i == selectedCount - 1)
                {
                    sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
                }
                else
                {
                    sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
                }
            }
            if (sbProIds.Length > 0)
            {
                theGoodsCard.UpdateEnable(1, sbProIds.ToString());
            }
            ExtAspNet.Alert.Show("批量开启商品卡片成功！");
            this.BindGrid();
        }

        protected void btnDisable_Click(object sender, EventArgs e)
        {
            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            StringBuilder sbProIds = new StringBuilder();
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];
                if (i == selectedCount - 1)
                {
                    sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
                }
                else
                {
                    sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
                }
            }
            if (sbProIds.Length > 0)
            {
                theGoodsCard.UpdateEnable(0,sbProIds.ToString());
            }
            ExtAspNet.Alert.Show("批量禁用商品卡片成功！");
            this.BindGrid();
        }

        protected void btnDeleted_Click(object sender, EventArgs e)
        {
            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            StringBuilder sbProIds = new StringBuilder();
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];
                if (i == selectedCount - 1)
                {
                    sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
                }
                else
                {
                    sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
                }
            }
            if (sbProIds.Length > 0)
            {
                theGoodsCard.DeleteList(sbProIds.ToString());
            }
            ExtAspNet.Alert.Show("批量删除商品卡片成功！");
            this.BindGrid();
        }



        protected void btnInputCard_Click(object sender, EventArgs e)
        {
            if (this.fuInput.FileName.Trim().Length < 1 )
            {
                ExtAspNet.Alert.Show("请添加要导入的EXCEL文件！");
                return;
            }
                try
                {
                    string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
                    this.fuInput.SaveAs(Server.MapPath("../uploadTempFile/" + fileName));
                    DataTable dtResult = new DataTable();
                    dtResult = Gift.Utile.ExcelHelper.ImportDataTableFromExcel(Server.MapPath("../uploadTempFile/" + fileName),"1",0);

                    DataRow[] dr = dtResult.Select();
                    int ss = dtResult.Rows.Count;
                    int rowsnum = dtResult.Rows.Count;
                    if (rowsnum == 0)
                    {
                          ExtAspNet.Alert.Show("Excel表为空表,无数据！");
                        return;
                    }
                    int iInportSum = 0;
                    int iUpdateSum = 0;
                    for (int i = 0; i < rowsnum; i++)
                    {
                        if (dr[i]["卡号"].ToString().Trim() == "")
                        {
                            continue;
                        }
                        string cardCode = dr[i]["卡号"].ToString();
                        string cardPass = Gift.Security.DESEncrypt.Encrypt(dr[i]["密码"].ToString());
                        string sMessageRemark = dr[i]["消息备注"].ToString();

                        oGoodsCard = theGoodsCard.GetModel(cardCode,cardPass);
                        if (oGoodsCard == null)
                        {
                            oGoodsCard = new tbGoodsCard();
                            oGoodsCard.addTime = DateTime.Now;
                            oGoodsCard.cardCode = cardCode;
                            oGoodsCard.cardPass = cardPass;
                            oGoodsCard.enable = true;
                            oGoodsCard.goodsId = int.Parse(Request.QueryString["gid"]);
                            oGoodsCard.messageRemark = sMessageRemark;
                            oGoodsCard.addAdminId = int.Parse(base.AdminUserId);
                            theGoodsCard.Add(oGoodsCard);
                            iInportSum++;
                        }else
                        {
                            oGoodsCard.messageRemark = sMessageRemark;
                            theGoodsCard.Update(oGoodsCard);
                            iUpdateSum++;
                        }
                    }
                    ExtAspNet.Alert.Show("成功导入" + iInportSum.ToString() + "条商品卡片信息,更新" + iUpdateSum.ToString() + "条商品卡片信息！");
                    this.fuInput.Text = "";
                    this.BindGrid();
                }
                catch (Exception ex)
                {
                    ExtAspNet.Alert.Show(ex.Message);
                }

        }



    }
}